/**
 * 影片剪辑2，序列帧为多张图片
 * @author zzhanglin
 * @param frameRate 播放速度
 * @param framesArray Bitmap数组或者BitmapData数组
 */
MovieClip2 = function( frameRate , framesArray)
{
	Bitmap.call(this);
	this.className="MovieClip2";
	this.frameRate = frameRate?frameRate:1 ;
	
	this._currentFrame =1 ; //当前播放到了第几帧，1为第一帧
	this._totalFrames = framesArray?framesArray.length:0 ; //总共的帧数
	this._frameArray =framesArray?framesArray: []; //所有的帧
	this._isPlay=false; //是否在自动播放状态
	this._tempFrameRate = 0 ;
};
FLASH.extend(MovieClip2,Bitmap); //继承自Bitmap

//==========getter/settter=========================================
MovieClip.prototype.isPlay = function(){ return  this._isPlay ;};
MovieClip.prototype.getCurrentFrame = function(){ return this._currentFrame ;};
MovieClip.prototype.getTotalFrames = function(){ return this._totalFrames ;};
//========================================================

/**
 * 重写设置图片数据源
 */
MovieClip.prototype.setBitmapData = function( bitmapData ) {
	//重写设置bitmapData，不能直接指定
};
/**
 *  添加帧
 *  @param bmp  Bitmap或者是BitmapData
 */
MovieClip2.prototype.addFrame = function( bmp )
{
	this._totalFrames++;
	this._frameArray.push(bmp);
	return this;	
};

/**
 * 一次设置所有的帧动作
 * @param rectArray
 */
MovieClip2.prototype.setFrames=function(framesArray)
{
	this._frameArray = framesArray ;
	this._totalFrames = framesArray.length;
};


/**
 * 私有方法，渲染一帧
 * @param context
 */
MovieClip2.prototype._draw =function( globalAlpha,context )
{
	if(this._totalFrames>0 ){
		if(this._currentFrame>this._totalFrames){
			this._currentFrame = 1 ;	
		};
		var mc = this._frameArray[this._currentFrame-1] ;
		this.setWidth( mc._width );
		this.setHeight(mc._height );
		//----------------------------------------------------------
		context.globalAlpha=globalAlpha ;
		context.setTransform( this._worldModelMatrix._a ,this._worldModelMatrix._b,this._worldModelMatrix._c,this._worldModelMatrix._d,
				this._worldModelMatrix._tx,this._worldModelMatrix._ty );
		if(mc instanceof Bitmap){
			context.drawImage(mc._bitmapData._image, 0 , 0 );
		} else if(mc instanceof BitmapData){
			context.drawImage(mc._image, 0 , 0 );
		}
		//-------------------------------------------------------------------
		if(this._isPlay){
			this._tempFrameRate++;
			if(this._tempFrameRate>=this.frameRate){
				this._tempFrameRate=0;
				this._currentFrame++;
			}
		}
	}
};
/**
 * 播放
 */
MovieClip2.prototype.play = function(){
	this._isPlay = true ;	
};
/**
 * 停止
 */
MovieClip2.prototype.stop = function(){
	this._isPlay = false ;	
};
/**
 * 到某一帧停止
 * @param frame
 */
MovieClip2.prototype.gotoAndStop = function(frame){
	if(frame<1) frame=1 ;
	else if(frame>this._totalFrames) frame=this._totalFrames;
	
	this._currentFrame = frame ;
	this._isPlay = false ;
};
/**
 * 到某一帧播放
 * @param frame
 */
MovieClip2.prototype.gotoAndPlay = function(frame){
	if(frame<1) frame=1 ;
	else if(frame>this._totalFrames) frame=this._totalFrames;
	
	this._currentFrame = frame ;
	this._isPlay = true ;
};
/**
 * 播放到下一帧 停止
 */
MovieClip2.prototype.nextFrame = function(){
	if(this._currentFrame==this._totalFrames) this._currentFrame=1;
	else this._currentFrame++;
};
/**
 * 转到上一帧停止
 */
MovieClip2.prototype.prevFrame = function(){
	if(this._currentFrame==1) this._currentFrame=this._totalFrames;
	else this._currentFrame--;
};
/**
 * 释放资源
 */
MovieClip2.prototype.dispose = function()
{
	Bitmap.prototype.dispose.call(this);
	this._frameArray =null; //所有的帧
};
